/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function altaComponente(){
     if(!validaAltaComponente())
           return false;
       $.ajax(
	{
		type: "POST",
		url: "altaComponente2",
		async: false,
                dataType: "text",
		data: $('#altaComponente').serialize(),
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
				alert(transport);
                                $("#body-content").html("");
                               
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
        $('#menuComponentes').trigger('click');
       return false;
}

function buscarComponentes(){
   $.ajax(
	{
		type: "GET",
		url: "buscarComponentes",
		async: false,
                dataType: "text",
                data: "buscar="+$("#buscar").val(),
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
				$("#body-content").html(transport);
                                $('input[id="btnVer"]').click(verComponente);
                                $('input[id="btnModificar"]').click(verModificarComponente);
                                $('input[id="btnEliminar"]').click(eliminarComponente);
                                $('#btnAgregar').click(agregarComponente);
                                $('#btnBuscar').click(buscarComponentes);
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
}

function agregarComponente(){
    $.ajax(
	{
		type: "GET",
		url: "buscarTipoComponente",
		async: false,
                dataType: "text",
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
                            $("#verComponente").html(transport);
                            $(".error-inner").hide();
                            $("#verComponente").show();
                            $("#tablaComponentes").hide();
                            $('#btnAltaComponente').click(altaComponente);
                            $("#btnRegresar").click(mostrarComponentes);
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
}

function verModificarComponente(){
     $.ajax(
	{
		type: "GET",
		url: "verModificarComponente",
		async: false,
                dataType: "text",
                data: "id="+$(this).parent().parent().attr("id"),
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
				$("#verComponente").html(transport);
                                $(".error-inner").hide();
                                $("#verComponente").show();
                                $("#tablaComponentes").hide();
                                $('#btnModificarComponente').click(modificarComponente);
                                $("#btnRegresar").click(mostrarComponentes);
				
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
}

function verComponente(){
    $.ajax(
	{
		type: "GET",
		url: "verComponente",
		async: false,
                dataType: "text",
                data: "id="+$(this).parent().parent().attr("id"),
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
				$("#verComponente").html(transport);
                                $("#verComponente").show();
                                $("#tablaComponentes").hide();
                                $("#btnRegresar").click(mostrarComponentes);
				
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
}

function eliminarComponente(){
    if(!confirm("Seguro que desea borrar el componente "+ $(this).parent().parent().attr("id")))
        return;
    id = $(this).parent().parent().attr("id");
    $.ajax(
	{
		type: "GET",
		url: "eliminarComponente",
		async: false,
                dataType: "text",
                data: "id="+id,
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
                            alert(transport); 
                            $("#"+id).remove()
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
}

function mostrarComponentes(){
    $("#verComponente").hide();
    $("#tablaComponentes").show();
    $("#verComponente").html("");
}

function validaAltaComponente(){
    $('.error-inner').hide();
    esValido = true;
    if($('#tipo').val()==""){
        $("#etipo").show();
        esValido = false;
    }
    if($('#caracteristicas').val()==""){
        $("#ecaracteristicas").show();
        esValido = false;
    }
    if($('#estatus').val()==""){
        $("#eestatus").show();
        esValido = false;
    }
    if(isNaN($("#cantidad").val()) || $("#cantidad").val().length < 1){
        $("#ecantidad").show();
        esValido = false;
    }
    return esValido;
}

function validaModificarComponente(){
    $('.error-inner').hide();
    esValido = true;
    if($('#tipo').val()==""){
        $("#etipo").show();
        esValido = false;
    }
    if($('#caracteristicas').val()==""){
        $("#ecaracteristicas").show();
        esValido = false;
    }
    if($('#estatus').val()==""){
        $("#eestatus").show();
        esValido = false;
    }
    return esValido;
}

function modificarComponente(){
    if(!validaModificarComponente())
           return false;
      $.ajax(
	{
		type: "POST",
		url: "modificarComponente",
		async: false,
                dataType: "text",
		data: $('#modificarComponente').serialize(),
                beforeSend: function(){
                    
                },
		success: function(transport)
		{
				alert(transport);
                                $("#body-content").html("");   
		},
                error: function (xhr, ajaxOptions, thrownError) {
                    $('#errores').html(xhr.responseText);
                    alert(thrownError);
                 }            
	});
        $('#menuComponentes').trigger('click');
       return false;   
}